Online-Academy
Look, Read, Understand, Apply

Lambda Map Filter Reduce

from functools import reduce
if __name__ == '__main__':
    x = lambda a , b : a + b
    print(x(4,5))

    str  = lambda s1, s2 : s1 +" " + s2
    print(str("ABC","124"))

    max_value = lambda x , y : x if x > y else y
    print(max_value(-1, 2))

    mix_value = lambda x , y : x if x < y else y
    print(mix_value(1, 200))

    def add(a,b):
        return a+b
    # map function takes function and collection of items
    # that function is applied to all the items
    x = map(add,[1,2,3],[4,5,6])
    print(x)
    print(list(map(add,[1,2,3],[4,5,6])))

    def cel_to_feh(celsius):
        return celsius * 1.8 + 32
    feh = map(cel_to_feh,[1,2,3,4,5,6])
    for x in feh:
        print(x)
    feh = list(map(cel_to_feh,[33,29,25,21,228]))
    print(feh)

    # filter function returns all items that pass the condition
    x = filter(lambda x : x > 4, [1,2,3,4,5,6])
    print(list(x))

    x = filter(lambda x : len(x) > 4, ("hello","cnn","world"))
    print(list(x))

    def strlen(ss : str):
        if len(ss)  > 4:
            s = x
        else:
            s = ""
        return s
    x = list(filter(strlen, ["hello","cnn","world"]))
    print(x)

    #reduce function reduces a list of values into a single value

    x = reduce(lambda x , y : x +y,(2,3,4,5))
    print(x)

    def addd(a,b):
        return a+b
    sum = reduce(addd,[1,2,3,4,5,6])
    print(f"{sum}")